package com.insleton.admin.image.service.impl;

import com.insleton.admin.image.model.Images;
import com.vteba.tx.dao.Page;
import com.vteba.tx.dao.impl.HibernateDaoImpl;
import com.vteba.utils.id.IdsGenerator;
import com.vteba.utils.reflection.BeanUtils;
import com.vteba.utils.serialize.protobuf.ProtoLiteUtils;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import com.vteba.common.service.impl.DefaultServiceImpl;
import com.insleton.admin.image.dao.spi.ImageResourceDao;
import com.insleton.admin.image.model.ImageResource;
import com.insleton.admin.image.service.spi.ImageResourceService;
import jakarta.transaction.Transactional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图片资源Service实现，基于 Hibernate DAO。
 *
 * @author 尹雷
 * @since 2025年6月17日 10:30:55
 */
@Named
@Transactional
public class ImageResourceServiceImpl extends DefaultServiceImpl<ImageResource, String>
		implements ImageResourceService {

	private static final Logger LOGGER = LogManager.getLogger(ImageResourceServiceImpl.class);

	private ImageResourceDao imageResourceDao;
	@Inject
	private IdsGenerator idsGenerator;

	@Inject
	@Override
	public void setBasicDao(HibernateDaoImpl<ImageResource, String> imageResourceDao) {
		this.imageResourceDao = (ImageResourceDao) imageResourceDao;
		this.hibernateDao = imageResourceDao;
	}

	@Override
	public void test() {
		ImageResource imageResource = new ImageResource();
		imageResource.setId(idsGenerator.get());
		imageResource.setUpdateDate(new Date());
		imageResource.setTitle("ts");
		imageResource.setImageUrl("https://123.com");
		imageResource.setComments("test");
		imageResource.setRemark("hibernate");

		String id1 = imageResourceDao.save(imageResource);
		// 最好是复制对象，或者重新new对象，因为hibernate一级缓存会缓存对象。直接修改id，再次保存和查询会出现问题
		// 使用深拷贝是可以的，BeanUtils.copyOther(); BeanUtils.copyBean(); ProtoLiteUtils.copy()
		ImageResource imageResource2 = ProtoLiteUtils.copy(imageResource);
		// ImageResource imageResource2 = new ImageResource();
		// imageResource2.setId(idsGenerator.get());
		// imageResource2.setUpdateDate(new Date());
		// imageResource2.setTitle("ts");
		// imageResource2.setImageUrl("https://123.com");
		// imageResource2.setComments("test");
		// imageResource2.setRemark("hibernate");
		imageResource2.setId(idsGenerator.get());
		imageResourceDao.persist(imageResource2);

		ImageResource imageResource1 = imageResourceDao.get(id1);

		ImageResource uni = new ImageResource();
		uni.setId(imageResource.getId());
		// identifier of an instance of com.insleton.admin.image.model.ImageResource was altered from 202506171124215781000001 to 202506171124181401000000
		// 同一个session内，不需要这么查
		ImageResource imageResource21 = imageResourceDao.uniqueResult(uni);

		Map<String, Object> map = new HashMap<>();
		map.put("id", imageResource.getId());
		List<ImageResource> imageResourceList3 = imageResourceDao.queryList(map);

		ImageResource query = new ImageResource();
		query.setTitle("ts");
		List<ImageResource> imageResourceList = imageResourceDao.queryList(query);

		Images images = new Images();
		images.setTitle("ts");
		String sql = "select id, update_date, title, image_url from image_resource where title = :title";
		List<Images> imageResourceList2 = imageResourceDao.queryListSQL(sql, Images.class, images);

		String sql2 = "select id, update_date, title, image_url from image_resource where id = :id";
		String sql3 = "select count(*) from image_resource where title = ?";
		// int i2 = imageResourceDao.countBy(sql3, "ts");
		// int i3 = imageResourceDao.countBy(query);

		Page<ImageResource> page = new Page<>();
		ImageResource query2 = new ImageResource();
		query2.setId(id1);
		// Page<ImageResource> page2 = imageResourceDao.queryPageList(page, sql2, query2);

		Page<ImageResource> page1 = imageResourceDao.queryForPage(page, query2);

		Page<ImageResource> page3 = imageResourceDao.queryForPage(page, map);

		// Page<Images> imagesPage = new Page<>();
		// Images images2 = new Images();
		// images2.setTitle("ts");
		// String sql4 = "select id, update_date, title, image_url from image_resource where title = :title";
		// Page<Images> imagesPage1 = imageResourceDao.queryPageList(imagesPage, sql4, images2);
		//
		// List<Images> imagesList = imageResourceDao.query(sql4, images2);

		imageResource2.setTitle("你好");
		int j = imageResourceDao.updateById(imageResource2);

		ImageResource imageResource3 = new ImageResource();
		BeanUtils.copy(imageResource, imageResource3);
		BeanUtils.clone(imageResource);

		// ImageResource imageResource3 = new ImageResource();
		// imageResource3.setId(idsGenerator.get());
		// imageResource3.setUpdateDate(new Date());
		// imageResource3.setTitle("ts");
		// imageResource3.setImageUrl("https://123.com");
		// imageResource3.setComments("test");
		// imageResource3.setRemark("hibernate");
		imageResource3.setId(idsGenerator.get());
		imageResourceDao.persist(imageResource3);

		ImageResource updateSet = new ImageResource();
		updateSet.setUpdateDate(new Date());
		ImageResource where = new ImageResource();
		where.setId("250614200337455100010");

		// 这里更新，可能需要提交事物，导致上面的persist出错。
		int k2 = imageResourceDao.updateBatch(updateSet, where);

		Map<String, Object> whereMap = new HashMap<String, Object>();
		whereMap.put("id", "250614200337455100011");
		int k3 = imageResourceDao.updateBatch(updateSet, where);

		imageResourceDao.delete(where);

		int i5 = imageResourceDao.deleteById("250614200331638100008");

		int i6 = imageResourceDao.deleteBatch(where);
		// String deleteSQL = "delete from image_resource where id = :id";
		// int i7 = imageResourceDao.deleteBatch(deleteSQL, where);

		LOGGER.info("ImageResource 测试完毕");
	}
}
